package pannelli;
import it.jack.CryptMD5;
import it.jack.PannelloPrincipale;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;


import oggetti.Cliente;
import pannelli.main.PannelloClienti;
import pannelli.sub.SubPannelloCliente;

public class AggiungiCliente extends javax.swing.JInternalFrame implements InternalFrameListener{
	private JButton Aggiungi;
	private JButton Accettazione;
	private boolean puoiUscire=true;
	private Cliente added;

	
	public AggiungiCliente() {
		super("Aggiungi cliente",true,true,true,true);
		initGUI();
		addListener();
		this.addInternalFrameListener(this);
		this.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
	}

	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				Accettazione = new JButton();
				getContentPane().add(Accettazione, new AnchorConstraint(822, 922, 959, 741, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				Accettazione.setText("Accettazione");
				Accettazione.setPreferredSize(new java.awt.Dimension(121, 36));
				Accettazione.setEnabled(false);
			}
			{
				Aggiungi = new JButton();
				getContentPane().add(Aggiungi, new AnchorConstraint(822, 252, 959, 99, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				Aggiungi.setText("Aggiungi");
				Aggiungi.setPreferredSize(new java.awt.Dimension(102, 36));
			}
			{
				SubPannelloCliente.getInstance().deleteAllFields();
				getContentPane().add(SubPannelloCliente.getInstance(), new AnchorConstraint(1, 1001, 719, 1, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				SubPannelloCliente.getInstance().setPreferredSize(new java.awt.Dimension(384, 188));
			}
			this.setSize(688, 310);
			SubPannelloCliente.getInstance().riempiID();
			setVisible(true);
			centra();
			setResizable(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void centra(){
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(new Point((dimension.width - getSize().width) / 2, 
				(dimension.height - getSize().height) / 2 ));

	}
	protected String generaUeP(Cliente c){
		String tmp = CryptMD5.MD5(c.getCognome()+" - "+c.getNome()+" - "+c.getEmail()+" - "+c.getFiscale()+" - "+c.getId()+" - "+c.getIva()+" - "+c.getResidenza()+" - "+c.getTelefono());
		return tmp;
	}
	protected Cliente addUeP(Cliente c){
		String x = generaUeP(c);
		c.setUsername(x.substring(0,7));
		c.setPassword(x.substring(x.length()-8,x.length()-1));
		return c;
	}
	private void addListener(){
		Aggiungi.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				added = SubPannelloCliente.getInstance().getClienteFromFields();
				added = addUeP(added);
				if(new DB().addCliente(added)){
					JOptionPane.showMessageDialog(null, "Cliente aggiunto con successo!\nRicorda di cliccare su accettazione!");					
					Aggiungi.setEnabled(false);
					Accettazione.setEnabled(true);		
					SubPannelloCliente.getInstance().disableAllField();
					puoiUscire=false;
				}else
					JOptionPane.showMessageDialog(null, "Si e' verificato un errore con il database!");

			}
		});
		Accettazione.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				new AggiungiRiparazione(added,0);
                                SubPannelloCliente.getInstance().enableAllField();
			}
		});
	}
	@Override
	public void internalFrameActivated(InternalFrameEvent arg0) {
		PannelloClienti.getIstance().disableButtons();
	}
	@Override
	public void internalFrameClosed(InternalFrameEvent arg0) {
		
	}
	@Override
	public void internalFrameClosing(InternalFrameEvent arg0) {
		if(!puoiUscire){
			JOptionPane.showMessageDialog(null,"Non puoi uscire senza cliccare prima su \"Accettazione\"");
		}else if(puoiUscire){
			PannelloClienti.getIstance().activeButtons();
			dispose();
		}
	}
	@Override
	public void internalFrameDeactivated(InternalFrameEvent arg0) {
		
	}
	@Override
	public void internalFrameDeiconified(InternalFrameEvent arg0) {
		
	}
	@Override
	public void internalFrameIconified(InternalFrameEvent arg0) {
		
	}
	@Override
	public void internalFrameOpened(InternalFrameEvent arg0) {
		PannelloClienti.getIstance().disableButtons();
	}
}
